using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RubyController : MonoBehaviour
{

    private Animator animator;

    private Rigidbody2D rigidbody2d;

    // 生命值
    public int maxHealth = 5;  // 最大生命值
    private int currentHealth; // 当前生命值
    public int Health { get { return currentHealth; } }

    public float speed = 3.5f;     // 速度

    // 无敌时间
    public float invincibleTime = 2.0f;
    private bool isInvincible;
    private float invincibleTimer; // 计时器

    // ruby的朝向
    private Vector2 lookDireciton = new Vector2(0, -1);


    public GameObject bulletPrefab;

    // Start is called before the first frame update
    void Start()
    {
        // 帧率
        // Application.targetFrameRate = 10;
        rigidbody2d = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();

        currentHealth = maxHealth;

        isInvincible = true;
        invincibleTimer = invincibleTime;

    }

    // Update is called once per frame
    void Update()
    { 
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");

        Vector2 move = new Vector2(horizontal, vertical);

        // Approximately 近似值
        if (!Mathf.Approximately(move.x,0) || !Mathf.Approximately(move.y,0))
        {
            lookDireciton.Set(move.x, move.y);
            //lookDireciton = move;
            lookDireciton.Normalize();

        }
        animator.SetFloat("Look X", lookDireciton.x);
        animator.SetFloat("Look Y", lookDireciton.y);
        animator.SetFloat("Speed", move.magnitude);


        Vector2 position = rigidbody2d.position;
        //position.x += speed * horizontal * Time.deltaTime;
        //position.y += speed * vertical * Time.deltaTime;

        position += speed * move * Time.deltaTime;


        // 直接改位置会跟物理引擎产生冲突，导致抖动
        // transform.position = position;

        rigidbody2d.MovePosition(position);


        if (isInvincible)
        {
            invincibleTimer -= Time.deltaTime;
            if (invincibleTimer <= 0)
            {
                animator.SetBool("Launch", false);
                isInvincible = false;
            }
        }


        if (Input.GetKeyDown(KeyCode.H))
        {
            Launch();
        }

    }


    public void ChangeHealth(int amount)
    {
        if (amount < 0)
        {
            if (isInvincible) return;
            animator.SetBool("Launch", true);
            isInvincible = true;
            invincibleTimer = invincibleTime;
        }
        currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
        Debug.Log("Ruby的生命值："+currentHealth + "/" + maxHealth);
    }


    private void Launch()
    {

        GameObject bullet = Instantiate(bulletPrefab,rigidbody2d.position,Quaternion.identity);
        BulletController bulletController = bullet.GetComponent<BulletController>();

        bulletController.Launch(lookDireciton,300);

        animator.SetTrigger("Launch");
    }

}
